<template>
    <div class="mt-4">
        <div class="text-red-500 mb-2">注：添加后在文件管理中查看</div>
        <div class="mb-2 text-xl font-bold">{{ fileChangeTit }}</div>
        <ElementFormC
            ref="fileFormRef"
            :formConfig="formConfig"
            :formItemConfig="formConfigData"
            @handle="formHandle"
        >
        </ElementFormC>
    </div>
</template>

<script setup lang="ts">
import { http } from "@/plugins/axios";
import stepData from "./stepData";
import {
    formConfig,
    formItemConfig,
    getFileGroupOption,
    fileNowOptionCache,
} from "./addFileFormconfig";
import { FormDefineExpose } from "@/components/globalComponents/ElementFormC/form-component";
const emit = defineEmits(["next", "end", "setNextDisabled", "formHandle"]);
const fileFormRef = ref<FormDefineExpose>();
let formConfigData = formItemConfig(fileFormRef);
let fileChangeTit = ref<string>("");
let fileChangeSelectData: any = {};
// HACK
const init = async () => {
    emit("setNextDisabled", false);
    const res = await getFileGroupOption();

    if (stepData.value.fileType === "file") {
        const { file } = stepData.value;
        fileFormRef.value!.setFormOption([
            {
                key: "fileName",
                value: file.name,
                option: [
                    {
                        label: file.name,
                        value: file.name,
                    },
                ],
                disabled: true,
                filterable: false,
                remote: false,
                allowCreate: false,
            },
            {
                key: "fileType",
                value: file.raw.type,
            },
            {
                key: "fileSize",
                value: file.raw.size,
            },
            {
                key: "documentNum",
                value: (stepData.value.fineTuneData as any[]).length,
            },
            {
                key: "fileGroupId",
                option: res.data.map((e: any) => {
                    return {
                        label: e.groupName,
                        value: e.id,
                    };
                }),
            },
        ]);
    }
    if (stepData.value.fileType === "text") {
        fileFormRef.value!.setFormOption([
            {
                key: "fileName",
                value: "",
                disabled: false,
                filterable: true,
                remote: true,
                allowCreate: true,
            },
            {
                key: "fileType",
                value: "text",
            },
            {
                key: "fileSize",
                value: "无",
            },
            {
                key: "documentNum",
                value: (stepData.value.fineTuneData as any[]).length,
            },
            {
                key: "fileGroupId",
                option: res.data.map((e: any) => {
                    return {
                        label: e.groupName,
                        value: e.id,
                    };
                }),
            },
        ]);
    }
    if (stepData.value.fileType === "html") {
        fileFormRef.value!.setFormOption([
            {
                key: "fileName",
                value: "",
                disabled: false,
                filterable: true,
                remote: true,
                allowCreate: true,
            },
            {
                key: "fileType",
                value: "html",
            },
            {
                key: "fileSize",
                value: "无",
            },
            {
                key: "documentNum",
                value: (stepData.value.fineTuneData as any[]).length,
            },
            {
                key: "fileGroupId",
                option: res.data.map((e: any) => {
                    return {
                        label: e.groupName,
                        value: e.id,
                    };
                }),
            },
        ]);
    }
};
// HACK
const formHandle = (type: string, key: string, data: any, other: any) => {
    if (
        (stepData.value.fileType === "text" ||
            stepData.value.fileType === "html") &&
        type === "change"
    ) {
        if (key === "fileName") {
            fileChangeSelectData = fileNowOptionCache.value.find(
                (e: any) => e.fileName === data
            );
            if (fileChangeSelectData) {
                if (fileChangeSelectData.canAdd) {
                    fileChangeTit.value = "将添加到现有文件";
                    fileFormRef.value?.setFormOption([
                        {
                            key: "id",
                            value: fileChangeSelectData.id,
                        },
                        {
                            key: "fileName",
                            value: fileChangeSelectData.fileName,
                        },
                        {
                            key: "fileType",
                            value: fileChangeSelectData.fileType,
                        },
                        {
                            key: "fileSize",
                            value: fileChangeSelectData.fileSize,
                        },
                        {
                            key: "documentNum",
                            value: String(
                                Number(fileChangeSelectData.documentNum) +
                                    (stepData.value.fineTuneData as any[])
                                        .length
                            ),
                        },
                        {
                            key: "fileDesc",
                            value: fileChangeSelectData.fileDesc,
                            disabled: true,
                        },
                        {
                            key: "fileGroupId",
                            value: Number(fileChangeSelectData.fileGroupId),
                            disabled: true,
                        },
                    ]);
                } else {
                    fileChangeTit.value =
                        "当前文件名被占用且不可编辑，请重新编辑";
                    fileFormRef.value?.setFormOption([
                        {
                            key: "fileName",
                            value: "",
                        },
                    ]);
                }
            } else {
                fileChangeTit.value = "将创建新文件";
                fileFormRef.value?.setFormOption([
                    {
                        key: "id",
                        value: 0,
                    },
                    // HACK
                    {
                        key: "fileType",
                        value:
                            stepData.value.fileType === "text"
                                ? "text"
                                : stepData.value.fileType === "html"
                                ? "html"
                                : "",
                    },
                    {
                        key: "fileSize",
                        value: "无",
                    },
                    {
                        key: "documentNum",
                        value: (stepData.value.fineTuneData as any[]).length,
                    },
                    {
                        key: "fileDesc",
                        value: "",
                        disabled: false,
                    },
                    {
                        key: "fileGroupId",
                        value: "",
                        disabled: false,
                    },
                ]);
            }
        }
    } else {
        fileFormRef.value!.setFormOption([
            {
                key: "id",
                value: 0,
            },
        ]);
    }
};
const submit = () => {
    if (!fileFormRef.value!.submitForm()) {
        return;
    }
    http.request<any>({
        url: "/rag/writeDocumentsToVectorStore",
        method: "post",
        q_spinning: true,
        q_contentType: "json",
        data: {
            documents: stepData.value.fineTuneData,
            ragFile: {
                ...fileFormRef.value!.getFromValue(),
            },
        },
    })
        .then((res) => {
            if (res.data.indexOf("成功") !== -1) {
                ElMessage.success("插入成功");
                emit("end");
            } else {
                ElMessage.error("插入失败");
            }
        })
        .catch((err) => {
            console.log(err);
        });
};
defineExpose({ init, submit });
</script>
